package com.m3839.sdk.ym.util

import android.text.InputFilter
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View.OnLongClickListener
import android.view.View.OnTouchListener
import android.widget.EditText
import android.widget.TextView
import java.util.regex.Pattern

/**
 * Description:InputUtils
 *
 * @author 陈宝阳
 * @create 2020/4/23 17: 24
 */
object InputUtils {

    /**
     * 数字的正则
     */
    private const val DIGIT_DEFAULT_REGEX = "[0-9]+"

    fun digitLimit(): InputFilter {
        return InputFilter { source, start, end, dest, dstart, dend ->
            val p =
                Pattern.compile(DIGIT_DEFAULT_REGEX)
            val m = p.matcher(source.toString())
            if (!m.matches()) "" else source
        }
    }


    /**
     * 禁止输入框复制粘贴菜单
     */
    fun disableCopyAndPaste(editText: EditText?) {
        try {
            if (editText == null) {
                return
            }
            editText.setOnLongClickListener(OnLongClickListener { true })
            editText.isLongClickable = false
            editText.setOnTouchListener(OnTouchListener { _, event ->
                if (event.action == MotionEvent.ACTION_DOWN) {
                    // setInsertionDisabled when user touches the view
                    setInsertionDisabled(editText)
                }
                false
            })
            editText.customSelectionActionModeCallback = object :
                ActionMode.Callback {
                override fun onCreateActionMode(
                    mode: ActionMode,
                    menu: Menu
                ): Boolean {
                    return false
                }

                override fun onPrepareActionMode(
                    mode: ActionMode,
                    menu: Menu
                ): Boolean {
                    return false
                }

                override fun onActionItemClicked(
                    mode: ActionMode,
                    item: MenuItem
                ): Boolean {
                    return false
                }

                override fun onDestroyActionMode(mode: ActionMode) {}
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun setInsertionDisabled(editText: EditText) {
        try {
            val editorField =
                TextView::class.java.getDeclaredField("mEditor")
            editorField.isAccessible = true
            val editorObject = editorField[editText]

            // if this view supports insertion handles
            val editorClass =
                Class.forName("android.widget.Editor")
            val mInsertionControllerEnabledField =
                editorClass.getDeclaredField(
                    "mInsertionControllerEnabled"
                )
            mInsertionControllerEnabledField.isAccessible = true
            mInsertionControllerEnabledField[editorObject] = false

            // if this view supports selection handles
            val mSelectionControllerEnabledField =
                editorClass.getDeclaredField(
                    "mSelectionControllerEnabled"
                )
            mSelectionControllerEnabledField.isAccessible = true
            mSelectionControllerEnabledField[editorObject] = false
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}